package org.dromara.iot.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.iot.domain.vo.IotElevatorEventVo;
import org.dromara.iot.domain.bo.IotElevatorEventBo;
import org.dromara.iot.service.IIotElevatorEventService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 电梯事件
 *
 * @author lsl
 * @date 2025-04-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/elevatorEvent")
public class IotElevatorEventController extends BaseController {

    private final IIotElevatorEventService iotElevatorEventService;

    /**
     * 查询电梯事件列表
     */
    @SaCheckPermission("iot:elevatorEvent:list")
    @GetMapping("/list")
    public TableDataInfo<IotElevatorEventVo> list(IotElevatorEventBo bo, PageQuery pageQuery) {
        return iotElevatorEventService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯事件列表
     */
    @SaCheckPermission("iot:elevatorEvent:export")
    @Log(title = "电梯事件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotElevatorEventBo bo, HttpServletResponse response) {
        List<IotElevatorEventVo> list = iotElevatorEventService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯事件", IotElevatorEventVo.class, response);
    }

    /**
     * 获取电梯事件详细信息
     *
     * @param eventId 主键
     */
    @SaCheckPermission("iot:elevatorEvent:query")
    @GetMapping("/{eventId}")
    public R<IotElevatorEventVo> getInfo(@NotNull(message = "主键不能为空")
                                         @PathVariable Long eventId) {
        return R.ok(iotElevatorEventService.queryById(eventId));
    }

    /**
     * 新增电梯事件
     */
    @SaCheckPermission("iot:elevatorEvent:add")
    @Log(title = "电梯事件", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotElevatorEventBo bo) {
        return toAjax(iotElevatorEventService.insertByBo(bo, true));
    }

    /**
     * 修改电梯事件
     */
    @SaCheckPermission("iot:elevatorEvent:edit")
    @Log(title = "电梯事件", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotElevatorEventBo bo) {
        return toAjax(iotElevatorEventService.updateByBo(bo));
    }

    /**
     * 删除电梯事件
     *
     * @param eventIds 主键串
     */
    @SaCheckPermission("iot:elevatorEvent:remove")
    @Log(title = "电梯事件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{eventIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] eventIds) {
        return toAjax(iotElevatorEventService.deleteWithValidByIds(List.of(eventIds), true));
    }
}
